package com.cloudsigns.factorymanager2.project.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForBaseInfoBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForSizeBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedbackContentBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/8/5.
 */

public class SurveyOrderDetailForTeamModel implements ISurveyOrderDetailForTeamModel {

    @Override
    public void getSurveyOrderDetailForTeam(final long feedbackDate, final String itemId, final String orderId, final OnSurveyOrderDetaiForTeamlListener onSurveyOrderDetaiForTeamlListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String url = ProjectUtil.SURVEY_ORDER_DETAIL_FOR_TEAM + "?orderId=" + orderId;
                    if (!TextUtils.isEmpty(itemId))
                        url = url + "&itemId=" + itemId;

                    if (feedbackDate > 0)
                        url = url + "&feedbackDate=" + feedbackDate;

                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url);

                    HttpResponse response;

                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);


                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onSurveyOrderDetaiForTeamlListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONObject dataJsonObj = new JSONObject(json.getString("data"));
                            SurVeyOrderBean orderBean = new SurVeyOrderBean();

                            String adminConfirmCompleteDateStr = dataJsonObj.getString("adminConfirmCompleteDate");
                            if (!TextUtils.isEmpty(adminConfirmCompleteDateStr) && !adminConfirmCompleteDateStr.equals("null")) {
                                orderBean.setAdminConfirmCompleteDate(Long.parseLong(adminConfirmCompleteDateStr));
                            }

                            orderBean.setAssignReceiveAccountId(dataJsonObj.getString("assignReceiveAccountId"));
                            orderBean.setAssignReceiveAccountName(dataJsonObj.getString("assignReceiveAccountName"));

                            String assignReceiveOrderTimeStr = dataJsonObj.getString("assignReceiveOrderTime");
                            if (!TextUtils.isEmpty(assignReceiveOrderTimeStr) && !assignReceiveOrderTimeStr.equals("null")) {
                                orderBean.setAssignReceiveOrderTime(Long.parseLong(assignReceiveOrderTimeStr));
                            }
                            String assignStatusStr = dataJsonObj.getString("assignStatus");
                            if (!TextUtils.isEmpty(assignStatusStr)) {
                                if (assignStatusStr.equals("CREATED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.CREATED);
                                } else if (assignStatusStr.equals("EDITED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.EDITED);
                                } else if (assignStatusStr.equals("SEND")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.SEND);
                                } else if (assignStatusStr.equals("RECEIVED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.RECEIVED);
                                } else if (assignStatusStr.equals("RETURNED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.RETURNED);
                                } else if (assignStatusStr.equals("COMPLETED")) {
                                    orderBean.setAssignStatus(CommonEnum.AssignStatus.COMPLETED);
                                }


                            }

                            String beginDateStr = dataJsonObj.getString("beginDate");
                            if (!TextUtils.isEmpty(beginDateStr) && !beginDateStr.equals("null")) {
                                orderBean.setBeginDate(Long.parseLong(beginDateStr));
                            }

                            orderBean.setCompanyId(dataJsonObj.getString("companyId"));
                            orderBean.setCompanyId(dataJsonObj.getString("companyName"));


                            String completeDateStr = dataJsonObj.getString("completeDate");
                            if (!TextUtils.isEmpty(completeDateStr) && !completeDateStr.equals("null")) {
                                orderBean.setCompleteDate(Long.parseLong(completeDateStr));
                            }

                            String completeStatusStr = dataJsonObj.getString("completeStatus");
                            if (!TextUtils.isEmpty(completeStatusStr) && !completeStatusStr.equals("null")) {
                                if (completeStatusStr.equals("WORKING")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.WORKING);
                                } else if (completeStatusStr.equals("WORKER_COMPLETE")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.WORKER_COMPLETE);
                                } else if (completeStatusStr.equals("ADMIN_COMPLETE")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.ADMIN_COMPLETE);
                                } else if (completeStatusStr.equals("SUPERADMIN_COMPLETE")) {
                                    orderBean.setCompleteStatus(CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE);
                                }

                            }

                            orderBean.setContacts(dataJsonObj.getString("contacts"));
                            orderBean.setContactsPhone(dataJsonObj.getString("contactsPhone"));

                            String createDateStr = dataJsonObj.getString("createDate");
                            if (!TextUtils.isEmpty(createDateStr) && !createDateStr.equals("null")) {
                                orderBean.setCreateDate(Long.parseLong(createDateStr));
                            }

                            orderBean.setCreateOrderAccountId(dataJsonObj.getString("createOrderAccountId"));
                            orderBean.setCreateOrderAccountName(dataJsonObj.getString("createOrderAccountName"));
                            orderBean.setEngineeringAddress(dataJsonObj.getString("engineeringAddress"));
                            orderBean.setEngineeringName(dataJsonObj.getString("engineeringName"));

                            String feedbackContentStr = dataJsonObj.getString("feedbackContent");
                            if (null != feedbackContentStr && !TextUtils.isEmpty(feedbackContentStr) && !"null".equals(feedbackContentStr)) {
                                JSONObject feedbackContentJsonObj = new JSONObject(feedbackContentStr);
                                SurveyFeedbackContentBean surveyFeedbackContentBean = new SurveyFeedbackContentBean();
                                surveyFeedbackContentBean.setAccountId(feedbackContentJsonObj.getString("accountId"));
                                surveyFeedbackContentBean.setAccountName(feedbackContentJsonObj.getString("accountName"));

                                String baseInfoListStr = feedbackContentJsonObj.getString("baseInfoList");
                                if (null != baseInfoListStr && !TextUtils.isEmpty(baseInfoListStr)) {
                                    JSONArray baseInfoJsonArr = new JSONArray(baseInfoListStr);
                                    List<SurveyFeedBackForBaseInfoBean> baseInfoList = new ArrayList<>();
                                    for (int i = 0; i < baseInfoJsonArr.length(); i++) {
                                        JSONObject baseInfoJsonObj = (JSONObject) baseInfoJsonArr.get(i);
                                        SurveyFeedBackForBaseInfoBean surveyFeedBackForBaseInfoBean = new SurveyFeedBackForBaseInfoBean();
                                        surveyFeedBackForBaseInfoBean.setCategoryDesc(baseInfoJsonObj.getString("categoryDesc"));
                                        surveyFeedBackForBaseInfoBean.setDesc(baseInfoJsonObj.getString("desc"));
                                        surveyFeedBackForBaseInfoBean.setId(baseInfoJsonObj.getString("id"));

                                        baseInfoList.add(surveyFeedBackForBaseInfoBean);

                                    }

                                    surveyFeedbackContentBean.setBaseInfoList(baseInfoList);

                                }

                                String feedbackDate = feedbackContentJsonObj.getString("feedbackDate");
                                if (null != feedbackDate && !TextUtils.isEmpty(feedbackDate)) {
                                    surveyFeedbackContentBean.setFeedbackDate(Long.parseLong(feedbackDate));
                                } else {
                                    surveyFeedbackContentBean.setFeedbackDate(0L);
                                }

                                String handleDate = feedbackContentJsonObj.getString("handleDate");
                                if (null != handleDate && !TextUtils.isEmpty(handleDate) && !"null".equals(handleDate)) {
                                    surveyFeedbackContentBean.setHandleDate(Long.parseLong(handleDate));
                                } else {
                                    surveyFeedbackContentBean.setHandleDate(0L);
                                }

                                surveyFeedbackContentBean.setHandlerAccountId(feedbackContentJsonObj.getString("handlerAccountId"));
                                surveyFeedbackContentBean.setHandlerAccountName(feedbackContentJsonObj.getString("handlerAccountName"));
                                surveyFeedbackContentBean.setId(feedbackContentJsonObj.getString("id"));

                                String prepareListStr = feedbackContentJsonObj.getString("prepareList");
                                if (null != prepareListStr && !TextUtils.isEmpty(prepareListStr)) {
                                    JSONArray prepareJsonArr = new JSONArray(prepareListStr);
                                    List<SurveyFeedBackForBaseInfoBean> prepareList = new ArrayList<>();
                                    for (int i = 0; i < prepareJsonArr.length(); i++) {
                                        JSONObject prepareJsonObj = (JSONObject) prepareJsonArr.get(i);
                                        SurveyFeedBackForBaseInfoBean prepareBean = new SurveyFeedBackForBaseInfoBean();
                                        prepareBean.setCategoryDesc(prepareJsonObj.getString("categoryDesc"));
                                        prepareBean.setDesc(prepareJsonObj.getString("desc"));
                                        prepareBean.setId(prepareJsonObj.getString("id"));

                                        prepareList.add(prepareBean);

                                    }

                                    surveyFeedbackContentBean.setPrepareList(prepareList);

                                }

                                String sizeListStr = feedbackContentJsonObj.getString("sizeList");
                                if (null != sizeListStr && !TextUtils.isEmpty(sizeListStr)) {
                                    JSONArray sizeJsonArr = new JSONArray(sizeListStr);
                                    List<SurveyFeedBackForSizeBean> sizeList = new ArrayList<>();
                                    for (int i = 0; i < sizeJsonArr.length(); i++) {
                                        JSONObject sizeJsonObj = (JSONObject) sizeJsonArr.get(i);
                                        SurveyFeedBackForSizeBean sizeBean = new SurveyFeedBackForSizeBean();
                                        sizeBean.setArcHight(sizeJsonObj.getInt("arcHight"));
                                        sizeBean.setArcLength(sizeJsonObj.getInt("arcLength"));
                                        sizeBean.setContent(sizeJsonObj.getString("content"));
                                        sizeBean.setCorner(sizeJsonObj.getInt("corner"));
                                        sizeBean.setHight(sizeJsonObj.getInt("hight"));
                                        sizeBean.setHightDif(sizeJsonObj.getInt("hightDif"));
                                        sizeBean.setId(sizeJsonObj.getString("id"));
                                        String imgUrlListStr = sizeJsonObj.getString("imgUrlList");
                                        if (null != imgUrlListStr && !TextUtils.isEmpty(imgUrlListStr)) {
                                            JSONArray imgUrlJsonArr = new JSONArray(imgUrlListStr);
                                            List<String> imgUrlList = new ArrayList();
                                            for (int j = 0; j < imgUrlJsonArr.length(); j++) {
                                                String imgUrl = imgUrlJsonArr.getString(j);
                                                imgUrlList.add(imgUrl);
                                            }
                                            sizeBean.setImgUrlList(imgUrlList);
                                        }
                                        sizeBean.setSpecialHight(sizeJsonObj.getInt("specialHight"));
                                        sizeBean.setSpecialWidth(sizeJsonObj.getInt("specialWidth"));
                                        sizeBean.setWidth(sizeJsonObj.getInt("width"));

                                        sizeList.add(sizeBean);
                                    }

                                    surveyFeedbackContentBean.setSizeList(sizeList);

                                }


                                String typeStr = feedbackContentJsonObj.getString("type");
                                if (!TextUtils.isEmpty(typeStr)) {
                                    if (typeStr.equals("NORMAL")) {
                                        surveyFeedbackContentBean.setType(CommonEnum.FeedBackType.NORMAL);
                                    } else if (typeStr.equals("UNUSUAL")) {
                                        surveyFeedbackContentBean.setType(CommonEnum.FeedBackType.UNUSUAL);
                                    } else if (typeStr.equals("COMPLETED")) {
                                        surveyFeedbackContentBean.setType(CommonEnum.FeedBackType.COMPLETED);
                                    }

                                }


                                orderBean.setSurveyFeedbackContentBean(surveyFeedbackContentBean);
                            }


                            String finishDateStr = dataJsonObj.getString("finishDate");
                            if (!TextUtils.isEmpty(finishDateStr) && !finishDateStr.equals("null")) {
                                orderBean.setFinishDate(Long.parseLong(finishDateStr));
                            }

                            orderBean.setHasPauseOrder(dataJsonObj.getBoolean("hasPauseOrder"));
                            orderBean.setId(dataJsonObj.getString("id"));


                            String lastPauseDateStr = dataJsonObj.getString("lastPauseDate");
                            if (!TextUtils.isEmpty(lastPauseDateStr) && !lastPauseDateStr.equals("null")) {
                                orderBean.setLastPauseDate(Long.parseLong(lastPauseDateStr));
                            }

                            String latStr = dataJsonObj.getString("lat");
                            if (!TextUtils.isEmpty(latStr)) {
                                orderBean.setLat(Double.parseDouble(latStr));
                            }

                            String lonStr = dataJsonObj.getString("lon");
                            if (!TextUtils.isEmpty(lonStr)) {
                                orderBean.setLon(Double.parseDouble(lonStr));
                            }

                            orderBean.setProjectName(dataJsonObj.getString("projectName"));

                            String realCompleteDateStr = dataJsonObj.getString("realCompleteDate");
                            if (!TextUtils.isEmpty(realCompleteDateStr) && !realCompleteDateStr.equals("null")) {
                                orderBean.setRealCompleteDate(Long.parseLong(realCompleteDateStr));
                            }

                            orderBean.setReceiveAccountId(dataJsonObj.getString("receiveAccountId"));
                            orderBean.setReceiveAccountName(dataJsonObj.getString("receiveAccountName"));
                            String receiveOrderTimeStr = dataJsonObj.getString("receiveOrderTime");
                            if (!TextUtils.isEmpty(receiveOrderTimeStr) && !receiveOrderTimeStr.equals("null")) {
                                orderBean.setReceiveOrderTime(Long.parseLong(receiveOrderTimeStr));
                            }


                            String sendOrderTimeStr = dataJsonObj.getString("sendOrderTime");
                            if (!TextUtils.isEmpty(sendOrderTimeStr) && !sendOrderTimeStr.equals("null")) {
                                orderBean.setSendOrderTime(Long.parseLong(sendOrderTimeStr));
                            }

                            String statusStr = dataJsonObj.getString("status");
                            if (!TextUtils.isEmpty(statusStr)) {
                                if (statusStr.equals("CREATED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.CREATED);
                                } else if (statusStr.equals("SEND")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.SEND);
                                } else if (statusStr.equals("RECEIVED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RECEIVED);
                                } else if (statusStr.equals("RETURNED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.RETURNED);
                                } else if (statusStr.equals("COMPLETED")) {
                                    orderBean.setStatus(CommonEnum.EngineeringOrderStatus.COMPLETED);
                                }
                            }

                            String superAdminConfirmCompleteDateStr = dataJsonObj.getString("superAdminConfirmCompleteDate");
                            if (!TextUtils.isEmpty(superAdminConfirmCompleteDateStr) && !superAdminConfirmCompleteDateStr.equals("null")) {
                                orderBean.setSuperAdminConfirmCompleteDate(Long.parseLong(superAdminConfirmCompleteDateStr));
                            }

                            orderBean.setWorkerLeaderCompleteComment(dataJsonObj.getString("workerLeaderCompleteComment"));


                            onSurveyOrderDetaiForTeamlListener.surveyOrderDetailForTeamSuccessed(orderBean);

                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            onSurveyOrderDetaiForTeamlListener.surveyOrderDetailForTeamFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnSurveyOrderDetaiForTeamlListener {
        void surveyOrderDetailForTeamSuccessed(SurVeyOrderBean surVeyOrderBean);

        void surveyOrderDetailForTeamFailed(String desc);
    }
}
